home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig07_07.jar / Ch07 / Fig07_07 / Fig07_07.cpp next >
C/C++ Source or Header  |  1997-10-20  |  570b  |  32 lines

  1. // Fig. 7.7: fig07_07.cpp  
  2. // Using the this pointer to refer to object members.
  3. #include <iostream.h>
  4.  
  5. class Test {
  6. public:
  7.    Test( int = 0 );             // default constructor
  8.    void print() const;
  9. private:
  10.    int x;
  11. };
  12.  
  13. Test::Test( int a ) { x = a; }  // constructor
  14.  
  15. void Test::print() const   // ( ) around *this required
  16. {
  17.    cout << "        x = " << x
  18.         << "\n  this->x = " << this->x
  19.         << "\n(*this).x = " << ( *this ).x << endl;
  20. }
  21.  
  22. int main()
  23. {
  24.    Test testObject( 12 );
  25.  
  26.    testObject.print();
  27.  
  28.    return 0;
  29. }
  30.  
  31.  
  32.